unit uIniFiles;

interface

uses
  IniFiles, uErro;

type
  TIniFiles = class

    {$REGION '<< Atributes >>'}

      strict private class var _Instance: TIniFiles;

      private var m_File: TIniFile;

    {$ENDREGION}

    {$REGION '<< Methods >>'}

      public class function GetInstance(): TIniFiles;
      public class procedure FreeInstance();

      public function OpenFile(const a_File: String): RegErro;
      public function GetValue(const a_Session: String; const a_Value: String): String;

    {$ENDREGION}

  end;

implementation

uses
  SysUtils;

{$REGION '<< TIniFiles >>'}

class procedure TIniFiles.FreeInstance;
begin
  if Assigned(_Instance)
    then FreeAndNil(_Instance);
end;

class function TIniFiles.GetInstance: TIniFiles;
begin
  if not Assigned(_Instance)
    then _Instance := inherited Create as Self;

  Result := _Instance;
end;

function TIniFiles.OpenFile(const a_File: String): RegErro;
begin
  try
    m_File := TIniFile.Create(a_File);
  except on E: Exception
    do Result := TErro.New(True, 'Erro ao iniciar o arquivo INI: ' + E.Message);
  end;
end;

function TIniFiles.GetValue(const a_Session, a_Value: String): String;
begin
  Result := m_File.ReadString(a_Session, a_Value, '');
end;

{$ENDREGION}

end.
